﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UFC.Common;
using System.Xml;
using System.Data;
using UFC.DBUtility;
using System.Windows.Forms;
namespace ExportDataCollection
{
    public class ExportDataMethod
    {
        public void ExportContainerCheckData(string ContainerName)
        {
//            string strsql = string.Format(@"SELECT C.PRODUCTNAME,C.PRODUCTDESCRIPTION,C.CONTAINERNAME, 
//                   CSR.SPECNO,CSR.WORKFLOWSTEPNAME,SCP.STEPCHECKPOINTID,SCP.CHECKNO,SCP.NAME,
//                   SCP.CHECKPOINTNOTE, SCP.KEYCHECK,SCP.MEOPCHECK,SCP.QUALITYCHECK,
//                   SCPR.STEPCHECKPOINTRESULTID,SCPR.CHECKPOINTRESULTNOTE,SCPR.CREATEDATE
//              FROM CONTAINERSTEPRECORD CSR
//              LEFT JOIN CONTAINERCURRENTINFO C ON C.CONTAINERID = CSR.CONTAINERID
//              LEFT JOIN STEPCHECKPOINT SCP ON SCP.WORKFLOWSTEPID = CSR.WORKFLOWSTEPID
//              LEFT JOIN STEPCHECKPOINTRESULT SCPR ON SCPR.CONTAINERID = CSR.CONTAINERID
//               AND SCPR.STEPCHECKPOINTID = SCP.STEPCHECKPOINTID
//            WHERE C.CONTAINERNAME='{0}' ORDER BY  CSR.SPECNO,SCP.CHECKNO", ContainerName);
            string strsql = string.Format(@"SELECT C.PRODUCTNAME,C.PRODUCTDESCRIPTION,C.CONTAINERNAME, 
                   s.SPECNO,CSR.WORKFLOWSTEPNAME,SCP.STEPCHECKPOINTID,SCP.CHECKNO,SCP.NAME,
                   SCP.CHECKPOINTNOTE, SCP.KEYCHECK,SCP.MEOPCHECK,SCP.QUALITYCHECK,
                   SCPR.STEPCHECKPOINTRESULTID,SCPR.CHECKPOINTRESULTNOTE,SCPR.CREATEDATE
              FROM workflowstep CSR
              LEFT JOIN spec s ON s.specid=csr.specid
              LEFT JOIN CONTAINERCURRENTINFO C ON C.Workflowid = CSR.Workflowid
              LEFT JOIN STEPCHECKPOINT SCP ON SCP.WORKFLOWSTEPID = CSR.WORKFLOWSTEPID
              LEFT JOIN STEPCHECKPOINTRESULT SCPR ON SCPR.CONTAINERID = c.CONTAINERID
               AND SCPR.STEPCHECKPOINTID = SCP.STEPCHECKPOINTID
                        WHERE C.CONTAINERNAME like '{0}%' ORDER BY S.SPECNO,SCP.CHECKNO", ContainerName);
            DataTable dt = OracleHelper.QueryTable(strsql, null);
            if (dt != null && dt.Rows.Count > 0)
            {
                DataTable condt = dt.DefaultView.ToTable(true, "CONTAINERNAME");
                foreach (DataRow dr in condt.Rows)
                {
                    DataView dv =dt.DefaultView;
                    dv.RowFilter = string.Format("CONTAINERNAME='{0}'", dr["CONTAINERNAME"]);
                    DataTable _dt = dv.ToTable();
                    CreateXMLForContainerCheckData(_dt);
                }
            }
        }

        private void CreateXMLForContainerCheckData(DataTable dt)
        {
            XmlDocument doc = new XmlDocument();
            doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", ""));
            XmlNode ReqNode = doc.CreateElement("ReqMsg");

            XmlNode headerNode = doc.CreateElement("header");
            AddXmlNode(doc, headerNode, "sendSysName", "APP_MES");
            AddXmlNode(doc, headerNode, "rcvSysName", "APP_MES");
            XmlNode extNode = doc.CreateElement("extConfig");
            AddXmlNode(doc, extNode, "isReTry", "false");
            headerNode.AppendChild(extNode);

            ReqNode.AppendChild(headerNode);
            XmlNode documentNode = doc.CreateElement("document");
            AddXmlNode(doc, documentNode, "Part_Name", dt.Rows[0]["PRODUCTDESCRIPTION"].ToString());
            AddXmlNode(doc, documentNode, "Part_NO", dt.Rows[0]["PRODUCTNAME"].ToString());
            AddXmlNode(doc, documentNode, "Batch_NO", dt.Rows[0]["CONTAINERNAME"].ToString());

            XmlNode cNode = doc.CreateElement("ItemList");
            foreach (DataRow dr in dt.Rows)
            {
                XmlNode itemNode = doc.CreateElement("Item");

                AddXmlNode(doc, itemNode, "Step_No", dr["SPECNO"].ToString());
                AddXmlNode(doc, itemNode, "Step_Name", dr["WORKFLOWSTEPNAME"].ToString());

                if (dr.IsNull("STEPCHECKPOINTID"))
                {
                    itemNode.AppendChild(doc.CreateElement("Check_No"));
                    itemNode.AppendChild(doc.CreateElement("Check_Name"));
                    itemNode.AppendChild(doc.CreateElement("Check_Note"));
                    itemNode.AppendChild(doc.CreateElement("Check_KeyCheck"));
                    itemNode.AppendChild(doc.CreateElement("Check_MeopCheck"));
                    itemNode.AppendChild(doc.CreateElement("Check_QualityCheck"));
                    itemNode.AppendChild(doc.CreateElement("Measured_Value"));
                    itemNode.AppendChild(doc.CreateElement("Measured_Time"));
                }
                else
                {
                    AddXmlNode(doc, itemNode, "Check_No", dr["CHECKNO"].ToString());
                    AddXmlNode(doc, itemNode, "Check_Name", dr["NAME"].ToString());
                    AddXmlNodeWithCData(doc, itemNode, "Check_Note", dr["CHECKPOINTNOTE"].ToString());
                    AddXmlNode(doc, itemNode, "Check_KeyCheck", dr["KEYCHECK"].ToString());
                    AddXmlNode(doc, itemNode, "Check_MeopCheck", dr["MEOPCHECK"].ToString());
                    AddXmlNode(doc, itemNode, "Check_QualityCheck", dr["QUALITYCHECK"].ToString());

                    if (dr.IsNull("STEPCHECKPOINTRESULTID"))
                    {
                        itemNode.AppendChild(doc.CreateElement("Measured_Value"));
                        itemNode.AppendChild(doc.CreateElement("Measured_Time"));
                    }
                    else
                    {
                        AddXmlNodeWithCData(doc, itemNode, "Measured_Value", dr["CHECKPOINTRESULTNOTE"].ToString());
                        AddXmlNode(doc, itemNode, "Measured_Time", dr["CREATEDATE"].ToString());
                    }
                }
                cNode.AppendChild(itemNode);
            }
            documentNode.AppendChild(cNode);

            ReqNode.AppendChild(documentNode);
            doc.AppendChild(ReqNode);

            string rootpath = Application.StartupPath;
            string filepath = string.Format("{0}//xml//{1}_{2:yyyyMMddHHmmssfff}_BatchMesuredValue.xml", rootpath,
                dt.Rows[0]["CONTAINERNAME"].ToString().Replace("/","_"), DateTime.Now);
            doc.Save(filepath);
        }

        public void ExportContainerPairData(string ContainerName)
        {
            string strsql = string.Format(@"SELECT ID,FILENAME,PAIR_NO,
                       FAPAIR_CODE,FAPART_NAME,FAPART_NO, FABATCH_NO, FAMEASURED_VALUE,
                       PAIR_CODE,PART_NAME, PART_NO,BATCH_NO,MEASURED_VALUE,
                       CLEARANCE_FIT,PAIR_TYPE,PAIR_TIME,CREATEDATE
                  FROM PAIRRESULTDATA
                    WHERE FAPART_NO||'/'||FABATCH_NO='{0}' ORDER BY PAIR_NO,FILENAME", ContainerName);
            DataTable dt = OracleHelper.QueryTable(strsql, null);
            if (dt != null && dt.Rows.Count > 0)
                CreateXMLForContainerPairData(dt);

        }
        private void CreateXMLForContainerPairData(DataTable dt)
        {
            XmlDocument doc = new XmlDocument();
            doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", ""));
            XmlNode ReqNode = doc.CreateElement("ReqMsg");
           
            XmlNode headerNode = doc.CreateElement("header");
            AddXmlNode(doc, headerNode, "sendSysName", "APP_MES");
            AddXmlNode(doc, headerNode, "rcvSysName", "APP_MES");
            XmlNode extNode = doc.CreateElement("extConfig");
            AddXmlNode(doc, extNode, "isReTry", "false");
            headerNode.AppendChild(extNode);

            ReqNode.AppendChild(headerNode);
            XmlNode documentNode = doc.CreateElement("document");

            XmlNode cNode = doc.CreateElement("ItemList");
            foreach (DataRow dr in dt.Rows)
            {
                XmlNode itemNode = doc.CreateElement("Item");
                AddXmlNode(doc, itemNode, "Pair_No", dr["Pair_No"].ToString());
                AddXmlNode(doc, itemNode, "FaPair_Code", dr["FaPair_Code"].ToString());
                AddXmlNode(doc, itemNode, "FaPart_Name", dr["FaPart_Name"].ToString());
                AddXmlNode(doc, itemNode, "FaPart_NO", dr["FaPart_NO"].ToString());
                AddXmlNode(doc, itemNode, "FaBatch_NO", dr["FaBatch_NO"].ToString());
                AddXmlNode(doc, itemNode, "FaMeasured_Value", dr["FaMeasured_Value"].ToString());
                AddXmlNode(doc, itemNode, "Pair_Code", dr["Pair_Code"].ToString());
                AddXmlNode(doc, itemNode, "Part_NO", dr["Part_NO"].ToString());
                AddXmlNode(doc, itemNode, "Batch_NO", dr["Batch_NO"].ToString());
                AddXmlNode(doc, itemNode, "Measured_Value", dr["Measured_Value"].ToString());
                AddXmlNode(doc, itemNode, "Clearance_Fit", dr["Clearance_Fit"].ToString());
                AddXmlNode(doc, itemNode, "Pair_Type", dr["Pair_Type"].ToString());
                AddXmlNode(doc, itemNode, "Pair_Time", dr["Pair_Time"].ToString());
                cNode.AppendChild(itemNode );
            }
            documentNode.AppendChild(cNode);

            //  documentNode.AppendChild(doc.CreateCDataSection(strData));
            ReqNode.AppendChild(documentNode);
            doc.AppendChild(ReqNode);

            string rootpath = Application.StartupPath;

            string filepath = string.Format("{0}//xml//{1:yyyyMMddHHmmssfff}_PairResult.xml", rootpath, DateTime.Now);
            doc.Save(filepath);
        }


        private void AddXmlNode(XmlDocument doc, XmlNode itemNode, string Name, string innerText)
        {
            XmlNode itemCNode = doc.CreateElement(Name);
            itemCNode.InnerText = innerText;
            itemNode.AppendChild(itemCNode);
        }

        private void AddXmlNodeWithCData(XmlDocument doc, XmlNode itemNode, string Name, string innerText)
        {
            XmlNode itemCNode = doc.CreateElement(Name);
            itemCNode.AppendChild(doc.CreateCDataSection(innerText));
            itemNode.AppendChild(itemCNode);
        }
    }
}
